package mx.edu.itschapala.ssrp.bean;

import java.util.Map;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import mx.edu.itschapala.ssrp.ln.CatalogoLN;
import mx.edu.itschapala.ssrp.ln.ConnectLN;
import mx.edu.itschapala.ssrp.ln.SesionLN;
import mx.edu.itschapala.ssrp.modelo.Usuario;

@Named(value = "loginBean")
@ApplicationScoped
public class LoginBean {

    @EJB
    private ConnectLN connectLN;

    @EJB
    private CatalogoLN catalogoLN;

    @EJB
    private SesionLN sesionLN;

    private String username;
    private String password;

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {

    }

    public String accesar() {
        Usuario u = sesionLN.getUsuarioPorNcontrol(username);
        String destino = "indexLogin";
        FacesMessage msg;

        if (u != null) {
            if (u.getPassword().equals(password)) {
                ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                Map<String, Object> sessionMap = externalContext.getSessionMap();

                sessionMap.put("USUARIO_LOGUEADO", u);

                Usuario logueado = (Usuario) sessionMap.get("USUARIO_LOGUEADO");

                switch (logueado.getPerfil()) {
                    case "vinculacion":
                        destino = "indexVinculacion";
                        break;
                    case "division":
                        destino = "indexDivision";
                        break;
                    case "docente":
                        destino = "indexAsesor";
                        break;
                    case "alumno":
                        destino = getAccionAlumno(logueado);
                        break;
                    case "academia":
                        destino = "indexAcademia";
                        break;
                    case "administrador":
                        destino = "Admin";
                        break;
                    case "servicios":
                        destino = "indexEscolares";
                        break;
                }
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido/a", logueado.getNombre());
                username = "";
                password = "";
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "La contraseña es incorrecta", "Intente poner la contraseña correcta");
            }

        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "El id no se encuetra", "Puede que no este registrado");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        return destino;
    }

    public String getAccionAlumno(Usuario u) {
        String destino;

        if (catalogoLN.getProyectoPorAlumno(u.getNcontrol()) == null) {
            destino = "ProyectoLista";
        } else {
            destino = "indexAlumno";
        }

        return destino;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
